﻿using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Diagnostics;
using System.Threading;

public partial class StoredProcedures
{
    /// <summary>
    /// Check to see if AS is running on a server
    /// </summary>
    /// <returns>int 0 if succesful, -1 if error</returns>

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static int CheckServerPerformance(string olapServer, out float PerfCounter, out string ErrorDesc)
    {
        try
        {
            PerformanceCounter cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            cpuCounter.MachineName = olapServer;


            PerfCounter = cpuCounter.NextValue();
            Thread.Sleep(1000);
            PerfCounter = cpuCounter.NextValue();
            cpuCounter.Close();
            ErrorDesc = "";
            return 0;

        }
        catch (Exception e)
        {
            //Return fail
            PerfCounter = 0;
            ErrorDesc = e.ToString();
            return -1;
        }

    }

}